package com.tiwi.gmbdr.OpenHelper;

import java.util.List;

import com.tiwi.gmbdr.constant.DBTablesSql;
import com.tiwi.gmbdr.constant.Global;
import com.tiwi.gmbdr.model.User;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class UserOpenHelper  extends SQLiteOpenHelper {

	public static final String TABLE_NAME = "user";
	public static final String NO_USER = "no_user";
	public static final String NAMA_USER = "nama_user";
	public static final String SKOR = "skor";
	Context context = null;
	
	
	public UserOpenHelper(Context context, String name, CursorFactory factory,
			int version) {
		super(context, Global.DATABASE_NAME, null, Global.DATABASE_VERSION);		
		this.context = context;
	}
	public UserOpenHelper(Context context) {
		super(context, Global.DATABASE_NAME, null, Global.DATABASE_VERSION);		
		this.context = context;
	}
	@Override
	public void onCreate(SQLiteDatabase db) {
		List<String> tableSqlList = DBTablesSql.getUserTableStructureSql();
		for (String sql : tableSqlList) {
			db.execSQL(sql);
		}
		
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// TODO Auto-generated method stub
		
	}
	
	
	// untuk save user
	public void insertUser(User bean){
		SQLiteDatabase db = getWritableDatabase();
		ContentValues values = aplicationToContentValues(bean);
		
		try {
				System.out.println("insertOrThrow");
//				db.insertOrThrow(TABLE_NAME, null, values);	
				db.insert(TABLE_NAME, null, values);
					
		}catch (Exception e) {
			System.out.println("insertUser Error "+e);
		}
		finally {
			if (db.isOpen())
				db.close();
		}
	}

	
	
public ContentValues aplicationToContentValues(User bean){

		ContentValues cValues = new ContentValues();
		cValues.put(NO_USER, bean.getNo_user());
		cValues.put(NAMA_USER, bean.getNama_user());
		cValues.put(SKOR, bean.getSkor());
		
		return cValues;
	}

}
